package com.longrise.android.byjk.plugins.course.coursedetail.healthrisk.intro;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.text.Html;
import android.view.ViewGroup;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.plugins.course.coursedetail.healthrisk.HealthRiskActivity;
import com.longrise.android.byjk.plugins.course.coursedetail.healthrisk.event.RiskIntroEvent;
import com.longrise.android.byjk.utils.TimeHelper;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.common.base.BaseFragment;
import com.longrise.common.datasource.remote.LoadDataManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.w3c.dom.Text;

/**
 * Created by callxkj on 2018/1/14.
 * 风险管理师的 介绍tab
 */

public class HealthRiskIntroFragment extends BaseFragment {

    private String mCourseid;
    private TextView mTvname;
    private TextView mTvgrade;
    private TextView mTvperiod;
    private TextView mTvintro;

    public static HealthRiskIntroFragment getInstance(String courseid) {
        HealthRiskIntroFragment fragment = new HealthRiskIntroFragment();
        Bundle bundle = new Bundle();
        bundle.putString("courseid", courseid);
        fragment.setArguments(bundle);
        return fragment;
    }

    private void getIntentData() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            mCourseid = arguments.getString("courseid");
        }
    }


    @Override
    protected int getLayoutResourse(@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_healthrisk_intro;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        mTvname = (TextView) mRootView.findViewById(R.id.healthrisk_intro_title);
        mTvgrade = (TextView) mRootView.findViewById(R.id.healthrisk_intro_grade);
        mTvperiod = (TextView) mRootView.findViewById(R.id.healthrisk_intro_period);
        mTvintro = (TextView) mRootView.findViewById(R.id.healthrisk_intro_tv);
        getIntentData();
        toGetIntro();
    }


    private void toGetIntro() {
        EntityBean coursEntityBean = new EntityBean();
        coursEntityBean.set("cardno", UserInfor.getInstance().getUsersfzh());
        coursEntityBean.set("courseid", mCourseid);
        coursEntityBean.set("scoreuser", UserInfor.getInstance().getUserid());
        EntityBean argBean = new EntityBean();
        argBean.set("bean", coursEntityBean);
        LoadDataManager.getInstance()
                .callService2(null, UrlConstants.BaseUrl, "FL_getTrainIntroduce",
                        argBean, new LoadDataManager.OnRequestCallListener2() {
                            @Override
                            public void onSuccess(String key, String service, Object result) {
                                try {
                                    EntityBean bean = (EntityBean) result;
                                    Integer resultstate = bean.getInt("resultstate");
                                    if (resultstate == 1) {
                                        EntityBean resultbean = bean.getBean("result");
                                        parseResult(resultbean);
                                    } else {
                                        String resultdesc = bean.getString("resultdesc");
                                        showToast(resultdesc);
                                    }
                                } catch (Exception e) {

                                }
                            }

                            @Override
                            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

                            }


                            @Override
                            public void onFinished(String key, String service) {

                            }
                        });
    }


    private void parseResult(EntityBean bean) {
        String realpic = bean.getString("realpic");
        boolean ishavemyscore = bean.getBoolean("ishavemyscore");
        String name = bean.getString("name");
        String infosummary = bean.getString("infosummary");
        String avgscore = bean.getString("avgscore");

        ((HealthRiskActivity) getActivity()).setImage(realpic);
        ((HealthRiskActivity) getActivity()).setCourseHasEvaluate(ishavemyscore);

        mTvname.setText(name);
        mTvgrade.setText(Html.fromHtml("评分   " + "<font color='#333333'>" + avgscore + "</font>"));
        mTvintro.setText(infosummary);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void RiskIntroEv(RiskIntroEvent event) {
        if (event.getTotaltraintime() != 0) {
            double totaltraintime = event.getTotaltraintime();
            String courseTime = TimeHelper.formateTime(totaltraintime + "");
            mTvperiod.setText(Html.fromHtml("课时   " + "<font color='#333333'>" + courseTime + "</font>"));

        }
        if (event.isNeedRefresh()) { //刷新课程评分
            refresh();
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 刷新
     */
    public void refresh() {
        toGetIntro();
    }

}
